/* 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.server.user;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.hmvc4gwt.example.hmvcblog.server.repository.user.exceptions.UserSessionNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserEditDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLoginDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLogoutDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserSessionId;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.EMailInvalidException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginExistException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.LoginNotFoundException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.NotLoggedInException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.PasswordEmptyException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.exception.PasswordWrongException;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.IUserRemoteService;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.server.ApplicationContext;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.services.user.IUserService;

/**
 * 
 * @author jens.meiss
 */
public class UserRemoteServiceImpl extends RemoteServiceServlet implements IUserRemoteService {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private IUserService userService;

	public UserRemoteServiceImpl() {
		setUserService((IUserService) ApplicationContext.getBean("userService"));
	}

	public Long addUser(UserNewDTO userNewDTO) throws LoginExistException,
			PasswordEmptyException, LoginEmptyException, EMailInvalidException {
		
		return getUserService().addUser(userNewDTO);
		
	}

	private IUserService getUserService() {
		return userService;
	}

	public Long login(UserLoginDTO userLoginDTO) throws LoginEmptyException,
			PasswordEmptyException, LoginNotFoundException,
			PasswordWrongException {

		return getUserService().login(userLoginDTO);
	
	}

	public void logout(UserLogoutDTO userLogoutDTO) {

		getUserService().logout(userLogoutDTO);
		
	}

	public void removeUser(UserRemoveDTO userRemoveDTO) throws NotLoggedInException {

		getUserService().removeUser(userRemoveDTO);

	}

	private void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public UserEditDTO edit(UserSessionId sessionId) throws NotLoggedInException {
		
		try {
			return getUserService().getUserEditDTO(sessionId);
		} catch (UserSessionNotFoundException e) {
			throw new NotLoggedInException();
		}
	}

	public void update(UserEditDTO userEditDTO)
			throws NotLoggedInException, PasswordEmptyException,
			EMailInvalidException {
		
		try {
			getUserService().updateUserData(userEditDTO);
		} catch (UserSessionNotFoundException e) {
			throw new NotLoggedInException();
		}
		
	}

}
